package com.petsdar.controller;

import com.petsdar.pojo.KeeperAccount;
import com.petsdar.service.KeeperAccountService;
import com.petsdar.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/4
 * Time: 17:54
 * Description: keeperAccountService相关操作的controller层
 * Version: V1.0
 */

@RestController
public class KeeperAccountController {
    
    /** keeperAccountService对象的业务逻辑处理实例对象 */
    private KeeperAccountService keeperAccountService;

    @Autowired
    public void setKeeperAccountService(KeeperAccountService keeperAccountService) {
        this.keeperAccountService = keeperAccountService;
    }

    /**
     * 获取所有店长账号信息
     * @return
     */
    @GetMapping("/keeper")
    public String getKeeperAccount(){
        List<KeeperAccount> keeperAccount = keeperAccountService.getKeeperAccount();
        if (!keeperAccount.isEmpty()){
            return ResultUtil.success(keeperAccount);
        }
        return ResultUtil.fail();
    }

    /**
     * 添加新店长账号信息
     * @param ktJson 店长账号Json数据
     * @return
     */
    @PostMapping("/keeper")
    public String addKeeperAccount(@RequestBody String ktJson){
        boolean result = keeperAccountService.addKeeperAccount(ktJson);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 更新店长账号信息
     * @param ktJson 店长账号Json数据
     * @return
     */
    @PutMapping("/keeper")
    public String updateKeeperAccount(@RequestBody String ktJson){
        boolean result = keeperAccountService.updateKeeperAccount(ktJson);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 通过id删除店长账号信息
     * @param ktId 店长账号的ID
     * @return
     */
    @DeleteMapping("/keeper")
    public String delKeeperAccount(@RequestParam("keeperAccountId") String ktId){
        boolean result = keeperAccountService.delKeeperAccount(ktId);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }
}
